import axios,{type AxiosInstance , type AxiosResponse} from "axios"
import type { AxiosRequestConfig } from "axios"
import {ElMessage} from "element-plus"
let instance:AxiosInstance=axios.create({
    // baseURL:"/reactapi",
    timeout:5000
})
instance.interceptors.request.use((config)=>{
    let token = localStorage.getItem("token")
    let jwt = localStorage.getItem("jwt")
    if(token){
        (config.headers as any).token = token,
        (config.headers.Authorization as any) = jwt
    }
    return config
})
instance.interceptors.response.use((res:AxiosResponse)=>{
    if(res.data?.data?.token){
        localStorage.setItem("token",res.data.data.token)
    }
    ElMessage({
        message:res.data.message,
        type:res.data.code==="200"?"success":"error"
    })
    return res.data
})
export default instance;
